package com.zj.core.base

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.KeyEvent
import com.zj.core.interfaces.TextMessageIml
import com.zj.core.tools.VoiceAsrTools

/**
 * 日期：2019年6月21日
 * 公司：北京子杰宝贝科技有限公司
 * 地址：北京市朝阳区朝阳农场
 * 开发者：达选文
 * <p>
 * 主体功能：数据录入和按键监听
 */
@SuppressLint("Registered")
open class BaseVoiceActivity : BaseBleToothActivity(), TextMessageIml {
    private var mHandle:VoiceAsrTools?=VoiceAsrTools()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mHandle?.setVoiceImage(view.voiceImgView)?.setHandleListener(this)
    }

    /**
     * 因为Handler是单例
     * 模板弹窗的handle覆盖了当前activity的handle
     * 所以当弹窗消失时需要重新设置Handler
     */
    override fun dialogDismiss(){
        mHandle?.setVoiceImage(view.voiceImgView)?.setHandleListener(this)
    }

    /**
     * 贸人设备
     */
    override fun maorenClick() {
        rightClick()
    }

    /**
     * 调用百度语音
     */
    override fun rightClick(){
        mHandle?.onVoiceKeyDown(this,this.currentFocus.windowToken)
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        mHandle?.onKeyUp(keyCode,this)
        return false
    }

    /**
     * 百度ASR回调
     */
    override fun messageResult(msg: String) {
        voiceResourceResult(msg)
    }

    /**
     * 数据录入
     */
    override fun voiceResourceResult(mes: String) {
        val mStr=replaceChar(mes)
        when {
            mStr=="模板清单"||mStr=="模板列表"->{    //模板清单
                templateList()
            }
            templateContains(mStr) -> {                   //判断是否是模板
                isTemplate(mStr)
            }
            else -> {                                           //语音正常录入
                normalInput(mes)
            }
        }
    }

    open fun isTemplate(mes: String) {}
    open fun normalInput(mes: String) {}
    open fun templateList(){}

    private fun replaceChar(str:String):String{
        return str.replace(",","")
            .replace("，","")
            .replace(".","")
            .replace("。","")
    }

    /**
     * 判断语音内容是否是模板关键字
     *
     * @param orderStr
     */
    fun templateContains(orderStr: String): Boolean {
        for (mTitle in mTitleArr!!) {
            if (mTitle.meaning == orderStr) {
                mDialogType = mTitle.wouldstatus
                return true
            }
        }
        return false
    }
}